package com.liuxu.supernews;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.DisplayMetrics;
import android.util.Log;
import android.widget.CheckBox;

import java.lang.reflect.InvocationTargetException;
import java.util.List;


public class BackService extends Service {
//     private List<CheckBox> checkBoxList;
    public Thread MyThread;
    public utils Helper = new utils();
    public boolean run=true;
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return new Mybinder();
    }
    public boolean onUnBind(Intent intent) {
        return super.onUnbind(intent);
    }

    public void onCreate() {
        super.onCreate();
        run=true;
        Log.e("Service", "onCreate");

        }

    public void onDestroy() {
        Helper.setRun(false);
        run=false;
        super.onDestroy();
        Log.e("Service", "onDestroy");

    }
    public int onStartCommand(Intent intent, int flags, int startId) {
        final boolean choice[]=intent.getBooleanArrayExtra("data");
        MyThread=new Thread(new Runnable(){
            @Override
            public void run() {
                Read reader=new Read();
                DisplayMetrics dm = getResources().getDisplayMetrics();
                Helper.setScreenWidth(dm.widthPixels);
                Helper.setScreenHeight(dm.heightPixels);
                String[] appnames={"funnews", "eastnews", "newsmoremore", "youthnews", "weliseesee", "sohunews", "bullnews","haicao","shandianhezi","kankanzhuan","mayinews","hbnews","qukantianxia","newsbus","huashengnews","mangguokandian","taoxinwen","quicknews"};
                while(run){
                    Log.v("我在运行","我在运行");
                    for(int i=0;i<choice.length;i++){
                        if(choice[i]) {
                            try {
                                reader.getClass().getDeclaredMethod(appnames[i],utils.class).invoke(reader, Helper);
                            } catch (IllegalAccessException e) {
                                e.printStackTrace();
                            } catch (InvocationTargetException e) {
                                e.printStackTrace();
                            } catch (NoSuchMethodException e) {
                                e.printStackTrace();
                            }
                        }else{
                        }
                    }
                }
            }
        });
        MyThread.start();
        return super.onStartCommand(intent, flags, startId);
    }

    class Mybinder extends Binder {
        public void setdata(List<CheckBox> checkBoxList){
//            BackService.this.checkBoxList=checkBoxList;
        }
    }
}
